#####################################################
##  When running stand alone with the rails env
## uncomment this
#####################################################
## script/runner script/import/archive.rb
require "Pathname"
require "rexml/document"
require "Logger"
require 'csv'




  @TEST_MODE = true
  @logger = nil

   
  def set_logger()
    ##FIXME:: write this to a log file instead of standard out
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::INFO
  end
  
  def run()
    
    p1 = Pathname('C:\home\parussel\dev\education\thesis\sites')
    p1.children.each do |file|
      if file.directory? then
        #sites\crs60\output\owner_lists
        @logger.info("Site: " + file.to_s)
        load_projections(file.to_s + '/output/projections/', 1)
          
      end # if
    end # each do
  end #run

  def import_ownership_changes(ownership_change_directory, project_id)
    
    #sites\[sitename]\output\ownership_change\bug_list_#.txt
    
    Pathname(ownership_change_directory).children.each do |file|
      ## you have an ownership file for a bug list

      ## get the bug list name out of the file name
      @logger.info( "finding buglist: " + File.basename(file.to_s, ".txt"))      
      bug_list = BugList.find_by_name(File.basename(file.to_s, ".txt"))

      ## it is possible that someone created a bug list, made
      ## ownership changes, and then deleted the bug list 
      ## this would have left ownership_changes in the system
      ## we have to ignore those changes or we won't be able to
      ## tie them to a bug list
      if bug_list.nil?
        @logger.info( "Skipping import of ownship change for nonexistant bug list: " + File.basename(file.to_s, ".txt"))      
        next
      end
    
      ownership_change = BugOwnershipChange.new
     
      ownership_change.bug_list_id = bug_list.id

      ## loop over each line of the bug list ownerhsip change file
      file = File.new(file, "r")
      while (line = file.gets)
        @logger.info( "parsing: " + line )
        ownership_array = line.split(':')
        
        ## the format of the ownership change file is: 
        #   bugID:newowner:timestamp:admin_username
        ownership_change.bugid = ownership_array[0]
        ownership_change.new_owner_id = Person.find_or_create_by_username(ownership_array[1]).id
        ## we skip the time stamp in ownership_array[2] because we don't care
        ownership_change.assigner_id = Person.find_or_create_by_username(ownership_array[3]).id
      end
      file.close
      
      save_object(ownership_change, line)
    end
    
  end
  
    
    
    






  ## starting in the ownership direcotry this iterates 
  ## over all of the ownership files
  def loop_over_ownership_files(ownership_directory, project_id)
    
    
    Pathname(ownership_directory).children.each do |file|

      @logger.info( "parsing: " +file.to_s )

      # put the manager username in the DB
      # and get the manager_id
      @logger.info( "saving manager username: " + File.basename(file.to_s, ".txt"))      
      manager = Person.find_or_create_by_username(File.basename(file.to_s, ".txt"))
      
      CSV::Reader.parse(File.open(file.to_s, 'rb')) do |row|

        @logger.info( "username: " + row[0] )
        person = Person.find_or_create_by_username(row[0])
        @logger.info( "first: " + row[1] )
        person.first_name = row[1]
        @logger.info( "last: " + row[2] )
        person.last_name = row[2]
      
        #same this person 
        @logger.info( "saving person: " + person.username )
        person.save
        
        ## Save the persons relationship to their manager 
        ## and this project
        owner = Owner.new

        ## FIXME: project_id doesn't exist yet!!!
        owner.project_id = project_id
        owner.person_id = person.id
        owner.manager_id = manager.id
        
        @logger.info( "saving person as owner: " + person.username )        
        owner.save
        
        
      end

    end
  
  end
    

 def import_owners()

    p1 = Pathname(SITE_HOME_DIRECTORY)
    
    p1.children.each do |file|  #iterate over each site directory
    
      next if !file.directory?  ## don't parse non directories
      
      log.info(file + "\n") ## site directory name
      
      ##    owner list directory    ##
      owner_directory += OWNER_SUB_DIRECTORY
       
      ## go through each .txt file in the owners directory 
      owner_directory.children.each do |team_file|
        
        ## this is where we keep the list of people that have
        ## been added to the team
        team_list = Array.new
        
        # manager username in filename (strip out the file extension)
        # the file format is parussel.txt where "parussel" is the manager username and ".txt" is the extension
        manager_username = team_file.split(".")[0]
        log.info("Manager's Username: " + manager_username + "\n") 
        
        ## the manager's info is somewhere in the team file
        ## We will assign this value as soon as we come across 
        ## the user data that matches the username in the file name
        manager = nil
        
        ## save each person in this file to the DB
        team_file.each do  |line|
          
          # parse  username,first,last
          person_array = line.split(",")
          
          
          ## when I save subsequent people
          saved_person = get_saved_person(person_array)

          ## remember this person so we can record the team later
          team_list.push(saved_person)
          
          ## identify the manager
          if manager_username == saved_person.username then
            ## found the manager's info
            manager = saved_person
          end
          
        end  ## end parsing each line of a team file
        
        
        ## add the ownership mappings
        team_list.each do |person| 
        
          owner = Owner.new
          ## FIXME: project_id doesn't exist yet!!!
          owner.project_id = project_id
          owner.person_id = person.id
          
          owner.manager_id = manager.id
          owner.save
        end
        
      end  ##  end parsing each file in the owners directory
           
     end ## traversing site directories    
   end     ## def import_owners()    
          

  # returns the person identified by the array of info
  # username,first,last if the person already exists 
  # in the DB their object is returned
  # otherwise they are saved to the DB
  def get_saved_person(person_array) 
 
    existing_person = Person.find_by_username(person_array[0])
    if existing_person != nil
      ## FIXME: Confirm I'm not overwriting the people in the team_list 
      log.info("found existing person: " + existing_person.username + "\n") 
      return existing_person
    end
    
    ## otherwise save this person to the DB
    person = Person.new
    person.username = person_array[0]
    person.firstname = person_array[1]
    person.lastname = person_array[2]
    
    person.save
    reutrn person
    
  end
     
  def load_projections(projections_directory, project_id)
    
    @logger.info( "###############   PARSING PROJECTIONS   ###############" )

    Pathname(projections_directory).children.each do |team_directory|
      next if !team_directory.directory?  ## don't parse non directories
      
      ## say which directory we're parsing
      @logger.info('Parsing: ' + File.basename( team_directory.to_s))

      ## get the manager username from the team_folder
      ## FIXME: This is a wild ass guess I have no idea if this syntax is right
      manager_username = File.basename(team_directory.to_s).split("_")[0]

      @logger.info('Manager Username: "' + manager_username + '"')


      ## FIXME: this syntax is a crap shoot too
      manager = Person.find_by_username(manager_username)      


      ## manager is nice, but we actually need to Owner ID name 
      ## for the manager in this project
      ##FIXME: Need to figure out how to do a find lookup on 
      ## this is a wild ass guess!
      ## FIXME: I'm not sure I should be using the owner relationship here...
      #owner = Owner.find( :project_id => project_id, person_id => manager.id)
      ## decided to bail on the owner. served no purpose

      team_directory.children.each do |projection_file|
          
        ## get the bug list name
        bug_list = BugList.find_by_name(File.basename(projection_file.to_s, ".txt"))
        
        
        CSV::Reader.parse(File.open(projection_file.to_s, 'rb')) do |row|
          projection = Projection.new
          
          ## Add the known info about the bug list and the owner. 
          projection.bug_list_id = bug_list.id
          ## FIXME: I might change this to just be manager ID.
          projection.person_id = manager.id
          
          @logger.info( "date: " + Time.at(row[0].to_i).to_s )
          projection.date = Time.at(row[0].to_i)
          @logger.info( "bug_count: " + row[1] )
          projection.bug_count = row[1]
        
        end ## edn parsing this projection file
        
      end ## end looping through projection files in a team directory.
      
     end ## looping through team projection directories
     
   end ## load_projections
   

     
     
  def parse_projections
      
    p1 = Pathname(SITE_HOME_DIRECTORY)
    
    p1.children.each do |file|
    
      next if !file.directory?  ## don't parse non directories
      log.info(file + "\n") ## site directory name
      
      ##    owner list directory    ##
      projections_directory += 'output/projections'
       
      ## go through each folder in the projectoins directory
      ## It contains  a .txt file with projections for each 
      ## bug list
      projections_directory.children.each do |team_folder|
        
        ## get the manager username from the team_folder
        ## FIXME: This is a wild ass guess I have no idea if this syntax is right
        manager_username = team_folder.split("_")[0]
        
        ## FIXME: this syntax is a crap shoot too
        manager = Person.find_by_username(manager_username)
        
        team_folder.children.each do |projection_file|
          
          ## get the bug list name
          ## FIXME: there is no project object yet
          bug_list = BugList.find(name => projection_file.split(".")[0], project_id => project.id)
          
          ## parse each line of the projection file
          projection_file.each do |projection_line|
            
            current_projection  = Projection.new
            
            ## FIXME: do I need to reformat the date?
            current_projection.date = projection_line.split(",")[0]
            current_projection.bug_count = projection_line.split(",")[1]              
            current_projection.bug_list_id = bug_list.id
            
            
            ## FIXME: Should there be a project_person table?
            ## or should projections just use the person object for the manager?
            current_projection.person_id = manager.id
            
            
          end  ## projection_file.each do |projection|
          
          
        end ## team_folder.children.each do |projection_file|
        
        
        
      end ## projections_directory.children.each do  
      
    end ## traverse site directories  

  end  ## end parse_projections

  
    
  #  end
    
  # validate you have a site directory...

  # parse the site directory xml

  # parse queries.xml

  # parse the owner directories

  # parse the bug count history files

  # parse the projections

  # Parse the ownership changes

  #importer = ThesisImporter.new 
  
  #importer.get_site_data

  


  
  
  
  set_logger()
  run()
 